package com.iplant.ams.controller.dms.fault;

import com.iplant.ams.service.DFBService;
import com.iplant.ams.service.DRMService;
import com.iplant.ams.service.po.dms.fault.DMSFaultBase;
import com.iplant.ams.service.po.dms.fault.DMSFaultBaseBPM;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/DMSFaultBaseBPM")
public class DMSFaultBaseBPMController extends BasicController<DMSFaultBaseBPM> {
    private static final Logger logger = LoggerFactory.getLogger(DMSFaultBaseBPMController.class);

    @Autowired
    DFBService mDFBService;

    public DMSFaultBaseBPMController() {
        super(DMSFaultBaseBPM.class);
    }

    /**
     * 查询任务（网页版）
     */
    @GetMapping("/EmployeeAllWeb")
    public Object EmployeeAllWeb(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);

            if (!wParams.containsKey("Status")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSFaultBaseBPM>> wServiceResult =
                    mDFBService.BPM_QueryEmployeeAllWeb(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 故障知识库维护
     */
    @TraceAPI(value = "故障知识库维护")
    @PostMapping("/SubmitFaultBase")
    public Object SubmitFaultBase(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            DMSFaultBase wDMSFaultBase = CloneTool.Clone(wParam.get("data"), DMSFaultBase.class);
            int wTaskID = StringUtils.parseInt(wParam.get("TaskID"));
            if(wDMSFaultBase==null||wTaskID<=0){
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult =
                    mDFBService.DMS_SubmitFaultBase(wLoginUser, wDMSFaultBase,wTaskID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
